package com.cq.xjms.web.actions.sys;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.cq.xjms.model.Course;
import com.cq.xjms.model.Department;
import com.cq.xjms.model.Teacher;
import com.cq.xjms.service.CourseManager;
import com.cq.xjms.service.SysManager;
import com.cq.xjms.web.actions.BaseAction;

public class CourseAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	private static Logger log = Logger.getLogger(CourseAction.class);
	private CourseManager courseManager;
	private Course course;
	private String type;
	private List list;
	private List<Teacher> teachers;
	private List<Course> courses;
	private SysManager sysManager;
	private Map<String,Object> params = new HashMap<String,Object>();
	private String scoreyear;
	private String scoreterm;
	
	public String addCourse(){
		teachers = sysManager.find(Teacher.TEACHER_LIST);
		return SUCCESS;
	}
	public String edit(){
		params.put("id", course.getId());
		courses = sysManager.find(Course.COURSE_QUERYBYID,params);
		if(courses!=null && courses.size()>0){
			course = courses.get(0);
		}
		return SUCCESS;
	}
	public String saveCourse(){
		try{
			params.put("code",course.getCode());
			courses = sysManager.find(Course.COURSE_QUERY,params);
			if(courses==null || courses.size()==0){
				 if(course.getTeacher().getId()==null){
					 course.setTeacher(null);
				 }
				 sysManager.saveOrUpdate(course);
				 type="add";
			}else{
				 if(course.getId()!=null && course.getId().equals(courses.get(0).getId())){
					 if(course.getTeacher().getId()==null){
						 course.setTeacher(null);
					 }
					 sysManager.saveOrUpdate(course);
					 type="edit";
				 }else{
					 list = new ArrayList();
					 list.add("此课程编号已经存在！");
					 this.setActionErrors(list);
				 }
			}	
		}catch(Exception e){
			log.error("save course failed",e);
			return INPUT;
		}
		course = null;
//		teachers = sysManager.find(Teacher.TEACHER_LIST);
		return SUCCESS;
	}
	
	public String list(){
		if(course==null){
			course = new Course();
			course.setTeacher(new Teacher());
		}
		try{
			courses = courseManager.queryCourses(course.getName(),course.getCode(),course.getTeacher().getId(),getLimit());
			totalRows = courseManager.countCourse(course.getName(),course.getCode(),course.getTeacher().getId());
		}catch(Exception e){
			log.error("",e);
		}
		return SUCCESS;
	}

	public String delete(){
		courseManager.remove(Course.class, course.getId());
		return SUCCESS;
	}
	public String loadCourseJson(){
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("status", 1);
		courses = sysManager.find(Course.COURSE_QUERYBYSTATUS,params);
		JSONArray arr = new JSONArray();
		try{
			for(Course c : courses){
				JSONObject obj = new JSONObject();
				obj.put("id", c.getId());
				obj.put("name", c.getName());
				arr.add(obj);
			}
		}catch(Exception e){
			log.error("",e);
		}
		
	
		this.writeJSON(arr.toString());
		return NONE;
	}
	
	
	public String listCourseJson(){
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("status", course.getStatus());
		courses = sysManager.find(Course.COURSE_QUERYBYSTATUS,params);
		JSONArray arr = new JSONArray();
		try{
			for(Course c : courses){
				JSONObject obj = new JSONObject();
				obj.put("id", c.getId());
				obj.put("name", c.getName());
				arr.add(obj);
			}
		}catch(Exception e){
			log.error("",e);
		}
		
	
		this.writeJSON(arr.toString());
		return NONE;
	}
	
	
	public String loadCourse(){
		String yearterm="";
		 if(scoreyear !=null){
			 yearterm +=scoreyear;
		 }
		 if(scoreterm !=null){
			 yearterm +=scoreterm;
		 }
		try{
		courses = courseManager.queryCoursesBy(course.getStatus(),yearterm,getLimit());
		totalRows = courseManager.countCourseBy(course.getStatus(),yearterm);
		}catch(Exception e){
			log.error(""+e);
		}
		return SUCCESS;
	}
	
	public static Logger getLog() {
		return log;
	}
	public static void setLog(Logger log) {
		CourseAction.log = log;
	}
	
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	
	public List<Teacher> getTeachers() {
		teachers = sysManager.find(Teacher.TEACHER_LIST);
		return teachers;
	}
	public void setTeachers(List<Teacher> teachers) {
		this.teachers = teachers;
	}
	public SysManager getSysManager() {
		return sysManager;
	}
	public void setSysManager(SysManager sysManager) {
		this.sysManager = sysManager;
	}
	public Map<String, Object> getParams() {
		return params;
	}
	public void setParams(Map<String, Object> params) {
		this.params = params;
	}
	public List getList() {
		return list;
	}
	public void setList(List list) {
		this.list = list;
	}
	public CourseManager getCourseManager() {
		return courseManager;
	}
	public void setCourseManager(CourseManager courseManager) {
		this.courseManager = courseManager;
	}
	public Course getCourse() {
		return course;
	}
	public void setCourse(Course course) {
		this.course = course;
	}
	public List<Course> getCourses() {
		return courses;
	}
	public void setCourses(List<Course> courses) {
		this.courses = courses;
	}
	public String getScoreyear() {
		return scoreyear;
	}
	public void setScoreyear(String scoreyear) {
		this.scoreyear = scoreyear;
	}
	public String getScoreterm() {
		return scoreterm;
	}
	public void setScoreterm(String scoreterm) {
		this.scoreterm = scoreterm;
	}
	
}
